package cn.caishen.report.controller;

import cn.caishen.report.common.exceptions.BusinessException;
import cn.caishen.report.common.exceptions.ResponseResult;
import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.domain.report.ReportChart;
import cn.caishen.report.service.report.ReportChartService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 报表看板配置控制层
 *
 * @author LB
 * @description 报表看板配置控制层
 * @create 2022-01-17 15:55:40
 */
@RestController
@RequestMapping(value = "/reportChart")
@Slf4j
@Api(tags = "报表看板配置")
public class ReportChartController {

    private final ReportChartService reportChartService;

    public ReportChartController(ReportChartService reportChartService) {
        this.reportChartService = reportChartService;
    }

    /**
     * 报表看板配置分页查询列表
     *
     * @param page 页码
     * @param limit 单页条数
     * @param jsonStr 查询条件
     * @return LbMap
     */
    @GetMapping(value = "/list", name = "报表看板配置分页查询列表")
    @ApiOperation("报表看板配置分页查询列表")
    public LbMap list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false) String jsonStr){
        try {
            LbMap param = LbMap.fromObject(jsonStr);
            PageInfo<ReportChart> pages = reportChartService.getList(page, limit, param);
            log.info("报表看板配置分页查询列表成功");
            return ResponseResult.success("报表看板配置分页查询列表成功", pages.getList());
        }catch (BusinessException e){
            log.info("报表看板配置分页查询列表失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置分页查询列表失败：" + e.getMessage());
        }catch (Exception e){
            log.info("报表看板配置分页查询列表失败" + e.getMessage());
            return ResponseResult.fail("报表看板配置分页查询列表失败");
        }
    }

    /**
     * 报表看板配置查询列表
     *
     * @param jsonStr 查询条件
     * @return LbMap
     */
    @GetMapping(value = "/allList", name = "报表看板配置查询列表")
    @ApiOperation("报表看板配置查询列表")
    public LbMap allList(@RequestParam(required = false) String jsonStr){
        try {
            LbMap param = LbMap.fromObject(jsonStr);
            List<ReportChart> list = reportChartService.getAllList(param);
            log.info("报表看板配置查询列表成功");
            return ResponseResult.success("报表看板配置查询列表成功", list);
        }catch (BusinessException e){
            log.info("报表看板配置查询列表失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置查询列表失败：" + e.getMessage());
        }catch (Exception e){
            log.info("报表看板配置查询列表失败" + e.getMessage());
            return ResponseResult.fail("报表看板配置查询列表失败");
        }
    }

    /**
     * 报表看板配置通过编号查询
     *
     * @param primaryKey
     * @return LbMap
     */
    @GetMapping(value = "/getById", name = "报表看板配置通过编号查询")
    @ApiOperation("报表看板配置通过编号查询")
    public LbMap getById(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到报表看板配置编号");
            }

            ReportChart reportChart = reportChartService.getById(primaryKey);
            log.info("报表看板配置通过编号查询成功");
            return ResponseResult.success("报表看板配置通过编号查询成功", reportChart);
        }catch (BusinessException e){
            log.info("报表看板配置通过编号查询失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置通过编号查询失败：" + e.getMessage());
        }catch (Exception e){
            log.info("报表看板配置通过编号查询失败" + e.getMessage());
            return ResponseResult.fail("报表看板配置通过编号查询失败");
        }
    }

    /**
     * 修改报表看板配置（新增、修改）
     *
     * @param reportChart reportChart
     * @return LbMap
     */
    @PostMapping(value = "/edit", name = "修改报表看板配置")
    @ApiOperation("修改报表看板配置（新增、修改）")
    public LbMap edit(@RequestBody ReportChart reportChart){
        try {
            if (Objects.isNull(reportChart.getReportChartId()) || reportChart.getReportChartId()<=0){
                //新增
                reportChartService.saveOne(reportChart);
                log.info("报表看板配置新增成功");
            }else {
                //编辑
                reportChartService.update(reportChart);
                log.info("报表看板配置修改成功");
            }
            return ResponseResult.success("报表看板配置编辑成功");
        }catch (BusinessException e){
            log.info("报表看板配置编辑失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置编辑失败：" + e.getMessage());
        }catch (Exception e){
            log.info("报表看板配置编辑失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置编辑失败");
        }
    }

    /**
     * 根据id删除报表看板配置
     *
     * @param primaryKey id
     * @return LbMap
     */
    @PostMapping(value = "/delete", name = "删除报表看板配置")
    @ApiOperation("删除报表看板配置")
    public LbMap delete(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到报表看板配置编号");
            }
            reportChartService.delete(primaryKey);
            log.info("报表看板配置删除成功");
            return ResponseResult.success("报表看板配置删除成功");
        } catch (BusinessException e){
            log.info("报表看板配置删除失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置删除失败：" + e.getMessage());
        } catch (Exception e){
            log.info("报表看板配置删除失败：" + e.getMessage());
            return ResponseResult.fail("报表看板配置删除失败");
        }
    }
}